package com.oscar;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Vibrator;
import android.support.v4.app.NotificationCompat;

import com.google.android.gms.gcm.GoogleCloudMessaging;

public class MyBroadcastReceiver extends BroadcastReceiver {

	public static final int NOTIFICATION_ID = 1;
	private NotificationManager mNotificationManager;
	NotificationCompat.Builder builder;
	
	private static final String SPF_NAME = "settings";
    private static final String NOTIFICATION = "NOTIFICATION"; 
    private static final String NOTIFICATION_VIBRATE = "NOTIFICATION_VIBRATE"; 
    private static final String USERNAME_STORED = "USERNAME_STORED";
	Context ctx;

	@Override
	public void onReceive(Context context, Intent intent) {
		GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
		ctx = context;
		String messageType = gcm.getMessageType(intent);
		if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
			sendNotification("Send error: " + intent.getExtras().toString());
		} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
				.equals(messageType)) {
			sendNotification("Deleted messages on server: "
					+ intent.getExtras().toString());
		} else {
			String sReceived = intent.getStringExtra("msg").toString();
			int indexNameStore = sReceived.indexOf("###");
			String message = sReceived.substring(indexNameStore+3, sReceived.length());
			
			String nameReceiver = sReceived.substring(0,indexNameStore);
			SharedPreferences receiverPreferences = context.getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
			Boolean notificationSettings = receiverPreferences.getBoolean(NOTIFICATION, true);
			Boolean notificationVibrateSetttings = receiverPreferences.getBoolean(NOTIFICATION_VIBRATE, true);
			String userNameStored = receiverPreferences.getString(USERNAME_STORED, "");
			//sendNotification(message + "notification:" + notificationSettings + "vibrate" + notificationVibrateSetttings + "nameStore: "+ userNameStored+"nameReceiver: "+ nameReceiver);
			//Log.i("received", "ok nameStore: " + sReceived.substring(0,indexNameStore) + "mesage: " + message);
			//Log.i("received", "nameStore: " + StoreHelper.IdStore  + "settings: " + SettingsHelper.notification);
			
			if(userNameStored.equals("")==false&&nameReceiver.equals(userNameStored)==true)
			{
				if(notificationSettings==true)
				{
					if(notificationVibrateSetttings==true)
					{
						Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
						v.vibrate(500);
					}
					sendNotification(message);
				}
			}
		}
		setResultCode(Activity.RESULT_OK);
	}
	
	private void sendNotification(String msg) {
		mNotificationManager = (NotificationManager) ctx
				.getSystemService(Context.NOTIFICATION_SERVICE);

		PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
				new Intent(ctx, MainActivity.class), 0);

		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
				ctx).setSmallIcon(R.drawable.ic_launcher)
				.setContentTitle("You had message from your store.")
				.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
				.setContentText(msg);
		mBuilder.setContentIntent(contentIntent);
		mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

	}
}
